package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.codetable.CtSupplyType;
import com.hilaver.dzmis.product.ProductAccessoryZip;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class ProductZipServiceImpl extends AbstractBaseService {
	
	public final static String COLUMN_PI_ID = "productIdentification.id";
	
	public final static String COLUMN_ZIP_ID = "azId";
	
	public final static String COLUMN_ZIP_REFERENCE = "azReference";
	
	public final static String COLUMN_ZIP_HEAD_PHOTO = "headPhotoName";
	
	public final static String COLUMN_ZIP_BLOCK_PHOTO = "blockPhotoName";
	
	public final static String COLUMN_ZIP_COLLAR_TYPE = "ctCollarType";
	
	private CtServiceImpl ctServiceImpl;
	private BiAccessoryZipServiceImpl biZipService;
	
	public ProductZipServiceImpl(){
		this.ctServiceImpl = new CtServiceImpl();
		this.biZipService = new BiAccessoryZipServiceImpl();
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(ProductAccessoryZip.class.getName(), id);
		return super.delete(ProductAccessoryZip.class.getName(), id);
	}
	
	public String edit(ProductAccessoryZip obj) throws Exception {
//		ProductIdentification pi = (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), obj.getProductIdentification().getId());
//		Integer total = 0;
//		Set<OrderProduct> orderProducts = pi.getOrderProducts();
//		for (OrderProduct orderProduct : orderProducts) {
//			Set<OrderProductItem> orderProductItems = orderProduct.getOrderProductItems();
//			for (OrderProductItem orderProductItem : orderProductItems) {
//				total += (obj.getUnitNumber() * orderProductItem.getTotal());
//			}
//		}
//		obj.setTotal(total);
		String operation = null;
		if(obj.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}

	@Override
	public String get(int id) throws Exception {
		ProductAccessoryZip obj = (ProductAccessoryZip)this.dao.get(ProductAccessoryZip.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		xmlSB.append(this.getOtherInfoStr(obj));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAll(Integer piId) throws Exception{
		String [] filters = new String[1];
		filters[0] = COLUMN_PI_ID + "=" + piId;
		List<ProductAccessoryZip> objList = this.dao.getAll(ProductAccessoryZip.class.getName(), COLUMN_ID, "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (ProductAccessoryZip obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(this.getOtherInfoStr(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductAccessoryZip.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	private String getOtherInfoStr(ProductAccessoryZip zip) throws Exception{
		StringBuffer sb = new StringBuffer();
		
		sb.append("<" + COLUMN_ZIP_ID + ">");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(zip.getBiAccessoryZip().getId());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</" + COLUMN_ZIP_ID + ">");
		
		sb.append("<" + COLUMN_ZIP_REFERENCE + ">");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(zip.getBiAccessoryZip().getReference());
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</" + COLUMN_ZIP_REFERENCE + ">");
		
		if(zip.getBiAccessoryZip().getCustomerReference() != null){
			sb.append("<customerReference>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(zip.getBiAccessoryZip().getCustomerReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</customerReference>");
		}
		
		if(zip.getBiAccessoryZip().getFactoryReference() != null){
			sb.append("<factoryReference>");
			sb.append(Constants.XML_CDATA[0]);
			sb.append(zip.getBiAccessoryZip().getFactoryReference());
			sb.append(Constants.XML_CDATA[1]);
			sb.append("</factoryReference>");
		}
		
		String supplier = ""; //后加需求，根据 辅料类型确定是 factory,custoer还是DZ。hardcode
		String factoryNameCN = null; //只是一个标记，并不是真正的factoryName,只是为了页面显示方便。
		String factoryNameEN = null;
		if("01".equals(zip.getBiAccessoryZip().getCtSupplyType())){
			supplier = zip.getBiAccessoryZip().getBiFranceCustomer().getName();
			factoryNameCN = zip.getBiAccessoryZip().getBiFranceCustomer().getName();
			factoryNameEN = zip.getBiAccessoryZip().getBiFranceCustomer().getDepartment();
		}else if("02".equals(zip.getBiAccessoryZip().getCtSupplyType())){
			if(Constants.LOCALE_TYPE_EN.equals(locale)){
				supplier = zip.getBiAccessoryZip().getBiFactory().getNameEN();
			}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
				supplier = zip.getBiAccessoryZip().getBiFactory().getNameCN();
			}else{
				supplier = zip.getBiAccessoryZip().getBiFactory().getNameEN();
			}
			
		}else{
			supplier = "DZ";
			factoryNameCN = "DZ";
		}
		sb.append("<supplier>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(supplier);
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</supplier>");
		
		sb.append("<ctSupplyType>");
		sb.append(Constants.XML_CDATA[0]);
		sb.append(this.ctServiceImpl.getDescription(CtSupplyType.class.getName(), zip.getBiAccessoryZip().getCtSupplyType(), this.locale));
		sb.append(Constants.XML_CDATA[1]);
		sb.append("</ctSupplyType>");
		
		
		this.biZipService.setLocale(this.locale);
		this.biZipService.setRealFileDir(this.realFileDir);
		sb.append(this.biZipService.toXMLFragment(zip.getBiAccessoryZip()));
		
		return sb.toString();
	}

	public static void main(String[] args) throws Exception{
		ProductZipServiceImpl os = new ProductZipServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}
}



